package com.example.gulimall.order.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.example.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "	2021000117697858";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCgyuj0ax5mhjUhgb1r6bXe00U6pFQkQ81HxLbw4qU2qdYphZs3xL66NoqZgnJpCPV/s2OBcMHPcUDdJHbUJ+GlII8PAFQPokIS6ehIk+kNE1mOizsVQKIPaEBbVSVrS896VmMnZWS4WJXZHRQZJjk/2g1u8ksnfF9sRPrThiaSl7THRa5ciouefy4e54TNW4PzWY41KdyLID8eT0RthcsYqH/sha52jRf7sr0uyUdgXiVE6/DTKUXufOayp/1yg5ubBDiDq92C6UIXQ1bOPGMhF0HsF/OvdF8CTNATk8HJHYoG4ph8ay/1SrF1Lx6CtLBu3kNKd5KrFMseK0wMBy3nAgMBAAECggEBAJyVvdL41jwlimw8+oiyqmh6+jonecFvPeQcT8O6FAZt0p/Q+t2Ofea94q+n918OWHWp7sdJVZR8yBwTvJzS0c2LLYYAXdxzuUDOBWVYlWYkZ9yG/lyt+qRic/n9q1kYFM8gfaMJ/NIBgrxSsxXiwXHLGbXnwwSK/Cpl6pY/WCT8hb9hOOxsEsMgjh2mDJ+Tk5fJb9YUqfLegkxxh1q9KrvmqKlQ/rpGlTa9cik/VTugbRH3ucEiPcVwk5Ojre1ZQj75QgIRXMV8+5zR39nj+/d9yk42Iq7fYOG3xg69pr4u6Aai3Z0IsUXGk6DiC4lKd5PnE7jLRwQK6ShUB0c9e2ECgYEA5jxF8HidqSR2dp23ITkRr39ZqGSfVpiV/PBdq/UJae5lKStoODcs0FeGoLyud68L4Tqwawtc3dc3q5lvL7xt6paGNW8KuZ/ywn5/Lr52nnIe4usn9V3ljLnxycgZ70x3amhMpJwyBbwSWQPULiBNCCkCIRMfiKdaL1lU/QvqYn8CgYEAsslBmo2wgxQdAh6N1O+fImyt2uPhglCwJrccI4MlOi4uLHwh8jowPHwnhMwTtaoYOLgKbVldA18+xt7Si32w7s//B+u1zWL3xw9vMIKSje7sz9A7dKrvKY+deWvET7teZzhvH6dun9hDVqPTLPzTjc5I4rhYwP185WJ6lUNFsJkCgYEAg6CZ661JldX+1Dm+5QOXlNoeDCPa5Ms/lOhm5VSmGSXuN+dVIi6JNGOV2HeG124AQLgo/En2we21KioCYyw7Ow7rk8j0PMOxKnjfhX3mmxdriRggNCuGfTohfdck3Ka+7trKVqHmB1lKtBF4URauc6feS8J/GMV0zfUonbMeQUkCgYAl9/PEvN6Fl9npP0/jrtfe2bG6ULbtk/E056JEMG/RgUkxDVOwO4dO8Rb7btOouuzV7s/YFShZtPqCBYQ1N4ZB0U5aAkbjzWbNkYuNg81X8yu7nn9h8YNFEkjhJm/Udvg/d46x7D7kF2uFlNkeLrtG3E5CR+AhbDQACv/5fwXn0QKBgEzfMYrCdzyPyYMslVJGI07rZQvzhtX0QNLablAy34qU+6FdEZwbq1E3gk/0rPGzqYRhDEeut15kvqS6KdoDdtfX62NYq1XZOL0kq/9zULFFGWy2z5CzC/q5ZevoZe5ZnUg+phwAWNn9Q8H+ztacIXdamsOCQMr4EhmMZFUkXriX";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvHb8D4zbiB+quJRf879NnMst4dZ+EO2nOETHOzpFs33WAHfAs9MUuZksThhL/aDkykZaIL/1uvRTeogHXN5XIX7ps6uOVg6mdUP6FgueMC52mwmlIWVDndVQLY+9xee0b44UCGtW6VsbtRfOPYaXXgKKH68K3x0JV9pNdRPYsB2H8gXBI+58DnOVbiXB1jPlTNTqdN0BZRfjfrWHBJBifaI6b415RHLD/GYJI+NowZJS5u+fghx3Igm7iiH+izsWgc8xiUWR6iqR4g+Et9s1H12Ljg2RyX+27TsPZk7s7OgLfdkoXs4Hi15V0k/Llyg2wDbopo8eV+6Yp7QD/mmuFwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://4v18y40005.wicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://order.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";
    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
